package com.zds.passwordbox.data.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import com.zds.passwordbox.common.Constant;


public class DBHelper extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 1;

    private static DBHelper instance;
    private SQLiteDatabase database;


    public static synchronized DBHelper getInstance(@Nullable Context context, @Nullable String name) {
        if (instance == null) {
            instance = new DBHelper(context, name);
        }
        return instance;
    }

    private DBHelper(@Nullable Context context, @Nullable String name) {
        super(context, name, null, DATABASE_VERSION);
    }

    public SQLiteDatabase open() {
        database = this.getWritableDatabase();
        return database;
    }

    public void close() {
        if (database != null) {
            database.close();
        }
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // 提前创建系统需要的表
        String privateKeyTable = "create table " + Constant.PRIVATE_KEY_TABLE +
                "(id integer primary key autoincrement," +
                "`key` varchar(20), " +
                "secret varchar(200))";
        db.execSQL(privateKeyTable);
        String accountTable = "create table " + Constant.ACCOUNT_TABLE +
                "(id integer primary key autoincrement," +
                "`typeText` varchar(20), " +
                "username varchar(50), " +
                "secret varchar(200), " +
                "type int, " +
                "updateTime int)";
        db.execSQL(accountTable);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}
